package jogs.net.gameroomserver;

import java.io.BufferedReader;
import java.io.PrintStream;
import java.net.Socket;
import java.lang.Thread;

public class TableServerInfo extends Thread
{
  public String host;
  public int port; 
  public Socket socket; 
  public BufferedReader in;
  public PrintStream out;
  public String name; 
  public int clientCount; 
  public int status;
  public String gameType;
  private boolean connected;
  private String lastCommand; 
  private String lastDetails; 
  private boolean waiting = false; 
  private GameRoomServer grs;
  
  
  public TableServerInfo(String _host, int _port, Socket _s, BufferedReader _in, PrintStream _out, String _name, int _clientCount, GameRoomServer _grs)
  {
    host = _host; port = _port; 
    socket = _s; in = _in; out = _out; 
    name = _name; clientCount = _clientCount;
    grs = _grs;
    status = 0;
    gameType = "NONE";
    connected = false;
  }
  
  private void handleUpdate(String details)
  {
  	String[] info = details.split(",");
  	if(info.length != 4)
  		return;
  	this.clientCount = Integer.parseInt(info[1]);
  	this.status = Integer.parseInt(info[2]);
  	gameType = info[3];
  	grs.updateTableInfo(getInfoString());
  }
  
  public String getInfoString()
  {
  	String update = name;
  	update += "," + clientCount;
  	update += "," + status;
  	update += "," + gameType;
  	return update;
  }
  
  public void handle(String line)
  {
  	if(line == null)
  		return;
  	String details = null;
		String command = null;
	
		
		//all commands start with a @ and end with a : 
		int begin = line.indexOf('@');
		int end = line.indexOf(':');
		
		//if @ and : are not in message than it is not a command
		if(begin == -1 || end == -1)
		{
			//this.send("@ERROR:Not a valid command");
			return;
		}
		
		//get the command out of the message
		command = line.substring(begin, end);
		
		//if command has details
		if(line.length() > end+1)
			details = line.substring(end+1);
		
		if(command.equals("@UPDATE_INFO"))
		{
			handleUpdate(details);
			//this.clientCount = Integer.parseInt(details);
			//metaServer.updateRoomCount(this);
		}
    else if (command.equals("@OK_ADDKEYPASS"))
    {
      lastCommand = command; 
      lastDetails = details; 
    }
    else
      System.out.println("grsi unrecognized command: " + command); 
  }
  
  public void run()
  {
  	
  	String line = "";
  	connected = true;
  	try
  	{
  		while(connected && (line = in.readLine()) !=null)
  		{
  			handle(line);

        if (waiting)
          wakeup();

  		}
  	}
  	catch(Exception e)
  	{
  		
  	}
  	
  }
  
  public synchronized void waitForCommand()
  {
    try 
    {
      waiting = true; 
      while (waiting)
      {
        // This is an efficient way to make threads wait on something
        wait(); 
      }
    }
    catch (Exception e)
    {
      e.printStackTrace(); 
    }
  }

  public synchronized void wakeup()
  {
    try
    {
      // Wake up threads waiting for a command to come in
      waiting = false; 
      notify();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String getLastCommand() { return lastCommand; }
  public String getLastDetails() { return lastDetails; }
    
  public String toString()
  {
  	return name;
  }
}